using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.ApplicationBlocks.Data;

namespace Microsoft.SharedSource.SqlServer.MDDE.ManagementStudio
{
    public partial class CreateInstanceInformationTable : Form
    {
        string connectionString;

        public string TableName
        {
            get { return textBox1.Text; }
        }

        public CreateInstanceInformationTable(
            string connectionString)
        {
            this.connectionString = connectionString;
            InitializeComponent();
            ApplyEnableLogic();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            ApplyEnableLogic();
        }

        private void ApplyEnableLogic()
        {
            if (textBox1.Text == string.Empty)
                btnOk.Enabled = false;
            else
                btnOk.Enabled = true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            // Try creating the table.
            try
            {
                SqlHelper.ExecuteNonQuery(
                    connectionString,
                    CommandType.Text,
                    "CREATE TABLE [dbo].[" + TableName + "]( " +
                        "[TemplateName] [nvarchar](256) NOT NULL, " +
                        "[InstanceName] [nvarchar](256) NOT NULL, " +
                        "[ComponentName] [nvarchar](256) NOT NULL, " +
                        "[ComponentType] [nvarchar](256) NOT NULL, " +
                        "[ComponentUri] [nvarchar](4000) NOT NULL, " +
                        "[ColumnCollectionType] [nvarchar](256) NULL, " +
                        "[ColumnCollectionName] [nvarchar](256) NULL, " +
                        "[ColumnName] [nvarchar](256) NULL, " +
                        "[ScalarPropertyName] [nvarchar](256) NULL, " +
                        "[ScalarPropertyValue] [nvarchar](4000) NULL, " +
                        "[SourceColumnName] [nvarchar](256) NULL, " +
                        "[SourceColumnUsage] [bit] NULL, " +
                        "[DestinationColumnName] [nvarchar](256) NULL )");
            }
            catch (Exception ex)
            {
                MessageBox.Show(
                    "Cannot create instance information table.\n" +
                    ex.Message,
                    "Error",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);

                textBox1.Text = string.Empty;
            }

            this.Close();
        }
    }
}